﻿/* *********************************************
 * Crypto libary : Ceasar module
 * Christiaan Rakowski 20110824
 * This module will perform a basic Ceasar cipher on the input string
 * ********************************************/

using System;
using System.Linq;

namespace CryptoCs
{
	namespace Ciphers
	{
		public class Ceasar
		{
			private static readonly Char[] Data = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
			public static String Encipher(String input, int key)
			{
				return input.Aggregate("", (current, c) => current + Data[Maths.Mod((IndexOf(c) + key), Data.Length)]);

				//String result = "";
				//foreach (char c in input)
				//{
				//    result += Data[Maths.Mod((key + IndexOf(c)),Data.Length)];
				//}
				//return result;
			}
			public static String Decipher(String input, int key)
			{
				return Encipher(input, (key*-1));
			}
			private static int IndexOf(Char c)
			{
				for (int i = 0; i < Data.Length; i++)
				{
					if(Data[i] == c)
					{
						return i;
					}
				}
				return -1;
			}
		}
	}
}
